package com.leyou.search.controller;

import com.leyou.search.service.SearchService;
import com.leyou.search.utils.SearchRequest;
import com.leyou.search.utils.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SearchController {

    @Autowired
    private SearchService searchService;

    /**
     * 首先是个分页查询，其次查询条件中包含了前台的搜索条件
     *
     * @return
     */
    @PostMapping("page")
    public ResponseEntity<SearchResult> page(@RequestBody SearchRequest searchRequest) {
        SearchResult goodsPageResult = searchService.page(searchRequest);
        if (goodsPageResult != null && 0 != goodsPageResult.getItems().size() && null != goodsPageResult.getItems()) {
            return ResponseEntity.ok(goodsPageResult);
        }


        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
